<template>
  <div class="warning-popup">
    <div class="popup-header">
      <h3 class="popup-title">{{ currentWarningInfo?.info?.warningTitle || title || '预警信息' }}</h3>
      <button class="close-btn" @click="$emit('close')">
        <svg width="16" height="16" viewBox="0 0 16 16">
          <path d="M12 4L4 12M4 4L12 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
        </svg>
      </button>
    </div>

    <!-- 加载状态 -->
    <div v-if="!currentWarningInfo" class="loading-container">
      <div class="loading-spinner"></div>
      <p>正在加载预警信息...</p>
    </div>

    <!-- 预警内容 -->
    <div class="card-box" v-else>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-descriptions :column="2" class="largeScreen-descriptions">
            <el-descriptions-item label="预警类型">{{
              currentWarningInfo?.info?.typeName || currentWarningInfo?.warningType || '未知'
            }}</el-descriptions-item>
            <el-descriptions-item label="预警等级">
              <span :style="{ 'color': WarningLevelColor }">
                {{ WarningLevelName }}
              </span>
            </el-descriptions-item>
            <el-descriptions-item label="处置状态">
              <span>{{ currentWarningInfo?.info?.statusName || currentWarningInfo?.status || '未知' }}</span>
              <el-button
                type="primary"
                @click="goAlarmWork(currentWarningInfo?.info?.workOrderInfoUrl)"
                v-if="currentWarningInfo?.info?.workOrderInfoUrl"
                >查看预警工单</el-button
              >
            </el-descriptions-item>
            <el-descriptions-item label="发生时间">{{
              currentWarningInfo?.info?.createTime || currentWarningInfo?.warningTime || '未知'
            }}</el-descriptions-item>
            <el-descriptions-item label="持续时间">{{
              currentWarningInfo?.info?.durationTime || currentWarningInfo?.duration || '未知'
            }}</el-descriptions-item>
            <el-descriptions-item label="位置">{{
              currentWarningInfo?.info?.occurrenceAddress || currentWarningInfo?.location || '未知'
            }}</el-descriptions-item>
          </el-descriptions>
        </el-col>
      </el-row>
      <EarlyWarningTickets ref="EarlyWarningTicketsRef" :alarm-work-order-url="EarlyWarningUrl" />
    </div>
  </div>
</template>

<script setup lang="ts">
import EarlyWarningTickets from '@/components/EarlyWarningTickets/index.vue';
import { ref, computed, onMounted } from 'vue';
import { getPointInfo } from '@/api/earlyWarning';

interface Props {
  data: any;
  title?: string;
}

const props = defineProps<Props>();
const currentWarningInfo = ref<any>(null);

enum WarningLevel {
  VerySevere = '1', // 特别严重
  Severe = '2', // 严重
  RelativelySevere = '3', // 较重
  General = '4' // 一般
}

const EarlyWarningUrl = ref<string>('');
const EarlyWarningTicketsRef = ref();
//查询预警工单
const goAlarmWork = (url: string) => {
  EarlyWarningUrl.value = url;
  EarlyWarningTicketsRef.value.open();
};
const WarningLevelName = computed(() => {
  switch (currentWarningInfo.value?.info?.warningLevel) {
    case WarningLevel.VerySevere:
      return '特别严重';
    case WarningLevel.Severe:
      return '严重';
    case WarningLevel.RelativelySevere:
      return '较重';
    case WarningLevel.General:
      return '一般';
    default:
      return '未知';
  }
});
const WarningLevelColor = computed(() => {
  switch (currentWarningInfo.value?.info?.warningLevel) {
    case WarningLevel.VerySevere:
      return '#ff7070';
    case WarningLevel.Severe:
      return '#ffb444';
    case WarningLevel.RelativelySevere:
      return '#fff780';
    case WarningLevel.General:
      return '#00ffff';
    default:
      return '#9ca3af';
  }
});
onMounted(async () => {
  const { data } = await getPointInfo({ type: props.data.type, id: props.data.id });
  currentWarningInfo.value = data;
});
</script>

<style scoped lang="scss">
.warning-popup {
  background-image: url('@/assets/largescreen/xiangqing-kuang.png');
  background-size: 100% 100%;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  width: 700px;
  max-height: 260px;
  overflow: hidden;
  color: white;
  box-sizing: border-box;
  padding: 20px;
}

.popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0 20px 0;
  margin-bottom: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.popup-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #0be4f1;
}

.close-btn {
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: background-color 0.2s;

  &:hover {
    background: rgba(255, 255, 255, 0.1);
  }
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: white;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top: 3px solid #0be4f1;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.warning-content {
  max-height: calc(85vh - 120px);
  overflow-y: auto;
}

.warning-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 16px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.warning-level {
  display: flex;
  align-items: center;
  gap: 12px;
}

.level-badge {
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 600;

  .level-critical & {
    background: rgba(220, 38, 38, 0.2);
    color: #dc2626;
    border: 1px solid rgba(220, 38, 38, 0.3);
  }

  .level-high & {
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.3);
  }

  .level-medium & {
    background: rgba(245, 158, 11, 0.2);
    color: #f59e0b;
    border: 1px solid rgba(245, 158, 11, 0.3);
  }

  .level-low & {
    background: rgba(34, 197, 94, 0.2);
    color: #22c55e;
    border: 1px solid rgba(34, 197, 94, 0.3);
  }

  .level-unknown & {
    background: rgba(156, 163, 175, 0.2);
    color: #9ca3af;
    border: 1px solid rgba(156, 163, 175, 0.3);
  }
}

.warning-status {
  font-size: 12px;

  &.status-active {
    color: #ef4444;
  }

  &.status-resolved {
    color: #22c55e;
  }

  &.status-pending {
    color: #f59e0b;
  }

  &.status-unknown {
    color: #9ca3af;
  }
}

.warning-time {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}

.warning-details {
  margin-bottom: 24px;
}

.warning-descriptions {
  :deep(.el-descriptions__label) {
    color: rgba(255, 255, 255, 0.7);
    font-weight: 500;
  }

  :deep(.el-descriptions__content) {
    color: #fff;
  }
}

.warning-message {
  margin-bottom: 24px;

  h4 {
    margin: 0 0 12px 0;
    font-size: 16px;
    font-weight: 600;
    color: #0be4f1;
  }
}

.message-content {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 16px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  line-height: 1.6;
}

.risk-assessment {
  margin-bottom: 24px;

  h4 {
    margin: 0 0 12px 0;
    font-size: 16px;
    font-weight: 600;
    color: #0be4f1;
  }
}

.risk-content {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 16px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.risk-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;

  &:last-child {
    margin-bottom: 0;
  }
}

.risk-label {
  color: rgba(255, 255, 255, 0.7);
}

.risk-value {
  font-weight: 600;

  &.risk-critical {
    color: #dc2626;
  }

  &.risk-high {
    color: #ef4444;
  }

  &.risk-medium {
    color: #f59e0b;
  }

  &.risk-low {
    color: #22c55e;
  }

  &.risk-unknown {
    color: #9ca3af;
  }
}

.emergency-response {
  margin-bottom: 24px;

  h4 {
    margin: 0 0 12px 0;
    font-size: 16px;
    font-weight: 600;
    color: #0be4f1;
  }
}

.response-content {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 16px;
  border: 1px solid rgba(255, 255, 255, 0.1);

  ul {
    margin: 0;
    padding-left: 20px;

    li {
      margin-bottom: 8px;
      line-height: 1.6;

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}

.related-data {
  margin-bottom: 24px;

  h4 {
    margin: 0 0 12px 0;
    font-size: 16px;
    font-weight: 600;
    color: #0be4f1;
  }
}

.data-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.data-item {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  text-align: center;
}

.data-label {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  margin-bottom: 4px;
}

.data-value {
  font-size: 16px;
  font-weight: 600;
  color: #0be4f1;
}

.action-buttons {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.action-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.2);

  &:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: translateY(-1px);
  }

  &.primary {
    background: linear-gradient(45deg, #3b82f6, #6366f1);
    border: 1px solid rgba(59, 130, 246, 0.3);

    &:hover {
      background: linear-gradient(45deg, #2563eb, #4f46e5);
    }
  }
}

.card-box {
  min-width: 600px;
  min-height: 200px;
}

:deep(.largeScreen-descriptions) {
  .el-descriptions__label {
    color: rgba(255, 255, 255, 0.7);
    font-weight: 500;
  }

  .el-descriptions__content {
    color: #fff;
  }
}
</style>
